package leetcode;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class Solution {


    @Test
    public void isHappyTest(){
//        System.out.println(isHappy(1));
        System.out.println(isHappy(19));
        System.out.println(isHappy(2));
    }

    public boolean isHappy(int n) {
        List<Integer> intList = new ArrayList<>();
        int count = n;
        do{
            count = sumOfSquares(count);
            if( count == 1 ){
                return true;
            }
            if( intList.contains(count) ){
                return false;
            }
            intList.add(count);
        }while ( true );
    }

    // 平方和
    public int sumOfSquares(int num){
        String str = num + "";
        int count = 0;
        for(int i=0 ; i<str.length(); i++){
            int ch = Integer.parseInt(str.charAt(i)+"");
            count += ch * ch;
        }
        return count;
    }



    public String longestCommonPrefix(String[] strs) {
        if( strs.length == 0 ){
            return "";
        }
        if( strs.length == 1 ){
            return strs[0];
        }
        String str = strs[0];
        for(int i=0; i<str.length(); i++){
            String temp = str.substring(0,i+1);
            for(int j=1; j<strs.length; j++){
                if( !strs[j].startsWith(temp) ){
                    return str.substring(0,i);
                }
            }
        }
        return str;
    }

    @Test
    public void longestCommonPrefixTest(){
        String[] strs = new String[]{"flower","flower","flower","flower"};
        System.out.println(longestCommonPrefix(strs));
    }

    @Test
    public void substringTest(){
        String str = "aaabbcc";
        System.out.println(str.substring(0,0));
    }
}